﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Xml.Linq;
using deezer.sdk.windowsphone.Helpers;

namespace deezer.sdk.windowsphone
{
    public delegate void GetArtistByIdResponseEventHandler(object sender, EventArgsArtist e); //root
    public delegate void GetTopArtistByIdResponseEventHandler(object sender, EventArgsTopArtist e);
    public delegate void GetAlbumsArtistByIdResponseEventHandler(object sender, EventArgsAlbumsArtist e);
    public delegate void GetCommentsArtistByIdResponseEventHandler(object sender, EventArgsCommentsArtist e);
    public delegate void GetFansArtistByIdResponseEventHandler(object sender, EventArgsFansArtist e);
    public delegate void GetRelatedArtistByIdResponseEventHandler(object sender, EventArgsRelatedArtist e);
    public delegate void GetRadioArtistByIdResponseEventHandler(object sender, EventArgsRadioArtist e);

    public class Artist
    {
        HttpWebRequest request;

        public event GetArtistByIdResponseEventHandler GetArtistByIdResponse;

        public event GetTopArtistByIdResponseEventHandler GetTopArtistByIdResponse;
        public event GetAlbumsArtistByIdResponseEventHandler GetAlbumsArtistByIdResponse;
        public event GetCommentsArtistByIdResponseEventHandler GetCommentsArtistByIdResponse;
        public event GetFansArtistByIdResponseEventHandler GetFansArtistByIdResponse;
        public event GetRelatedArtistByIdResponseEventHandler GetRelatedArtistByIdResponse;
        public event GetRadioArtistByIdResponseEventHandler GetRadioArtistByIdResponse;

        protected virtual void GetArtistByIdResponseCheck(EventArgsArtist e)
        {
            if (GetArtistByIdResponse != null)
                GetArtistByIdResponse(this, e);
        }
        protected virtual void GetTopArtistByIdResponseCheck(EventArgsTopArtist e)
        {
            if (GetTopArtistByIdResponse != null)
                GetTopArtistByIdResponse(this, e);
        }
        protected virtual void GetAlbumsArtistByIdResponseCheck(EventArgsAlbumsArtist e)
        {
            if (GetAlbumsArtistByIdResponse != null)
                GetAlbumsArtistByIdResponse(this, e);
        }
        protected virtual void GetCommentsArtistByIdResponseCheck(EventArgsCommentsArtist e)
        {
            if (GetCommentsArtistByIdResponse != null)
                GetCommentsArtistByIdResponse(this, e);
        }
        protected virtual void GetFansArtistByIdResponseCheck(EventArgsFansArtist e)
        {
            if (GetFansArtistByIdResponse != null)
                GetFansArtistByIdResponse(this, e);
        }
        protected virtual void GetRelatedArtistByIdResponseCheck(EventArgsRelatedArtist e)
        {
            if (GetRelatedArtistByIdResponse != null)
                GetRelatedArtistByIdResponse(this, e);
        }
        protected virtual void GetRadioArtistByIdResponseCheck(EventArgsRadioArtist e)
        {
            if (GetRadioArtistByIdResponse != null)
                GetRadioArtistByIdResponse(this, e);
        }

        public void GetArtistByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/artist/{0}&output=xml", id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseIntern), null);
        }

        private void ByIdResponseIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsArtist e = new EventArgsArtist(Converter.ConvertJsonToTypeArtist(responseStream.ReadToEnd()));
                        GetArtistByIdResponseCheck(e);
                    }
                }
            }
        }

        public void GetTopArtistByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/artist/{0}/top&output=xml", id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseTopIntern), null);
        }

        private void ByIdResponseTopIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsTopArtist e = new EventArgsTopArtist(Converter.ConvertJsonToTypeTracks(responseStream.ReadToEnd()));
                        GetTopArtistByIdResponseCheck(e);
                    }
                }
            }
        }

        public void GetAlbumsArtistByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/artist/{0}/albums&output=xml", id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseAlbumsIntern), null);
        }

        private void ByIdResponseAlbumsIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsAlbumsArtist e = new EventArgsAlbumsArtist(Converter.ConvertJsonToTypeAlbums(responseStream.ReadToEnd()));
                        GetAlbumsArtistByIdResponseCheck(e);
                    }
                }
            }
        }

        public void GetCommentsArtistByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/artist/{0}/comments&output=xml", id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseAlbumsCommentsIntern), null);
        }

        private void ByIdResponseAlbumsCommentsIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsCommentsArtist e = new EventArgsCommentsArtist(Converter.ConvertJsonToTypeComments(responseStream.ReadToEnd()));
                        GetCommentsArtistByIdResponseCheck(e);
                    }
                }
            }
        }

        public void GetFansArtistByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/artist/{0}/fans&output=xml", id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseFansIntern), null);
        }

        private void ByIdResponseFansIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsFansArtist e = new EventArgsFansArtist(Converter.ConvertJsonToTypeFans(responseStream.ReadToEnd()));
                        GetFansArtistByIdResponseCheck(e);
                    }
                }
            }
        }

        public void GetRelatedArtistByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/artist/{0}/related&output=xml", id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseRelatedIntern), null);
        }

        private void ByIdResponseRelatedIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsRelatedArtist e = new EventArgsRelatedArtist(Converter.ConvertJsonToTypeRelated(responseStream.ReadToEnd()));
                        GetRelatedArtistByIdResponseCheck(e);
                    }
                }
            }
        }

        public void GetRadioArtistByIdAsync(Int32 id)
        {
            Uri uriId = new Uri(String.Format(@"http://api.deezer.com/2.0/artist/{0}/radio&output=xml", id));
            request = HttpWebRequest.CreateHttp(uriId);
            request.Method = "GET";
            request.BeginGetResponse(new AsyncCallback(ByIdResponseRadioIntern), null);
        }

        private void ByIdResponseRadioIntern(IAsyncResult result)
        {
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
                    using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                    {
                        EventArgsRadioArtist e = new EventArgsRadioArtist(Converter.ConvertJsonToTypeTracks(responseStream.ReadToEnd()));
                        GetRadioArtistByIdResponseCheck(e);
                    }
                }
            }
        }
    }
}
